# 1、使用{}创建
# 2、使用内置函数dict()  还可映射函数zip()
import random

d = {1: 'cat', 2: 'dog', 3: 'pig', 4: 'chicken'}
print(d)

# d1 = [1, 2, 3, 4]
# d2 = ['red', 'blue', 'yellow', 'green']
# d3 = zip(d1, d2)
# print(d3)
# # print(list(d3)) # 转换成列表
# print(dict(d3))

# d4 = dict(cat=10, dog=20)
# print(d4)
#
# t = (11, 12, 13)  # 元组可以作为key
# print({t: 123})

# list1 = [11, 12, 13]  # 列表是可变序列，不可作为key
# print({list1: 123})  # TypeError: unhashable type: 'list'

# print(max(t))
# print(min(t))
# print(len(t))
#
# del t
# breakpoint()
# print(t)
#
# # 遍历
# print(d[1])
# print(d.get(2))
# # 区别：如果key不存在，d.[key] 报错，d.get(key)可以指定默认值
# # print(d[5])
# print(d.get(5), '不存在')
#
# for item in d.items():
#     print(item)
#
# # 分别获取key value
# for key, value in d.items():
#     print(key, value)
#
# # 通过赋值 添加元素
# d[5] = '闪电侠'
# print(d)
#
# keys = d.keys()
# print(keys)
# print(list(keys))
# print(tuple(keys))
#
# values = d.values()
# print(values)
# print(list(values))
# print(tuple(values))
#
# # 字典中的数据转为键值对形式
# list1 = list(d.items())
# print(list1)  #[(1, 'cat'), (2, 'dog'), (3, 'pig'), (4, 'chicken')]
#
# d = dict(list1)
# print(d)
#
# print(d.pop(1))
# print(d)
#
# # print(d.pop(5), '不存在')
#
# print(d.popitem())
# print(d)
#
# d.clear()
# print(d)

# print(bool(d))

# 字典生成式
# d = {item: random.randint(1, 100) for item in range(4)}
# print(d)
#
# list1 = [101, 102, 103]
# list2 = ['小宋', '小红', 'jack']
# d = {key: value for key, value in zip(list1, list2)}
# print(d)
